<?php

namespace addons\shopro\controller;


use think\Db;

class OrderAftersale extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $params = $this->request->get();

        $this->success('售后列表', \addons\shopro\model\OrderAftersale::getList($params));
    }


    /**
     * 详情
     */
    public function detail()
    {
        $params = $this->request->get();

        $this->shoproValidate($params, get_class(), 'detail');

        $this->success('售后详情', \addons\shopro\model\OrderAftersale::detail($params));
    }



    // 申请售后
    public function aftersale()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'aftersale');

        //开始-2022-2-7 Ripple 验证拼团商品申请售后
        $check_order = Db::name('shopro_order')
            ->where(['id'=>$params['order_id']])
            ->find();
        if (!empty($check_order['activity_type'])){
            $activity_type_array = explode(',',$check_order['activity_type']);
            if (in_array('groupon',$activity_type_array)){
                //判断拼团是否为无上限拼团
                $ext_array = json_decode($check_order['ext'],true);
                $groupon_id = $ext_array['groupon_id'];
                $groupon_info = Db::name('shopro_activity_groupon')->where(['id'=>$groupon_id])->find();
                if ($groupon_info['active_team_num'] != '0'){
                    $this->error('拼团订单售后请到拼团详情申请!由团长进行审核');
                }
            }
        }else{
            $ext_array = json_decode($check_order['ext'],true);
            $groupon_id = isset($ext_array['groupon_id']) ? $ext_array['groupon_id']:0;
            if (!empty($groupon_id)){
                $groupon_info = Db::name('shopro_activity_groupon')->where(['id'=>$groupon_id])->find();
                if (!(($groupon_info['status'] == 'finish') || ($groupon_info['status'] == 'finish-fictitious'))){
                    $this->error('拼团订单售后请到拼团详情申请!由团长进行审核');
                }
            }
        }
        //结束-2022-2-7 Ripple 验证拼团商品申请售后

        $this->success('申请成功', \addons\shopro\model\OrderAftersale::aftersale($params));
    }


    // 取消售后单
    public function cancel()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'cancel');

        $this->success('取消成功', \addons\shopro\model\OrderAftersale::operCancel($params));
    }

    // 删除售后单
    public function delete()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'delete');

        $this->success('删除成功', \addons\shopro\model\OrderAftersale::operDelete($params));
    }

}
